﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace JY.Wpf.Controls.Controls.Attachs
{
    /// <summary>
    /// DataGrid附加属性
    /// </summary>
    public class DataGridAttach
    {
        public static DependencyProperty SelectedItemsProperty = DependencyProperty.RegisterAttached("SelectedItems",
            typeof(IList), typeof(DataGridAttach),new FrameworkPropertyMetadata(default(IList), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedItemsChanged));

        public static IList GetSelectedItems(DependencyObject target) => (IList)target.GetValue(SelectedItemsProperty);

        public static void SetSelectedItems(DependencyObject target, IList value) => target.SetValue(SelectedItemsProperty, value);

        private static void OnSelectedItemsChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            if (!(target is DataGrid dataGrid)) return;
            if (e.NewValue is IList selectItems)
            {
                //反选
                foreach (var selectItem in selectItems)
                {
                    dataGrid.SelectedItems.Add(selectItem);
                }
            }
            dataGrid.SelectionChanged += (sender, arg) =>
            {
                var selectedItems = (IList)dataGrid.GetValue(DataGridAttach.SelectedItemsProperty);
                selectedItems.Clear();
                foreach (var selectItem in dataGrid.SelectedItems)
                {
                    selectedItems.Add(selectItem);
                }
            };
        }
        public static DependencyProperty ShowRowNumberProperty = DependencyProperty.RegisterAttached("ShowRowNumber",
            typeof(bool), typeof(DataGridAttach),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits, OnShowRowNumberChanged));

        public static bool GetShowRowNumber(DependencyObject target) => (bool)target.GetValue(ShowRowNumberProperty);

        public static void SetShowRowNumber(DependencyObject target, bool value) => target.SetValue(ShowRowNumberProperty, value);

        private static void OnShowRowNumberChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            if (!(target is DataGrid dataGrid)) return;
            var show = (bool)e.NewValue;

            if (show)
            {
                dataGrid.LoadingRow += DataGrid_LoadingRow;
                dataGrid.ItemContainerGenerator.ItemsChanged += ItemContainerGenerator_ItemsChanged;
            }
            else
            {
                dataGrid.LoadingRow -= DataGrid_LoadingRow;
                dataGrid.ItemContainerGenerator.ItemsChanged -= ItemContainerGenerator_ItemsChanged;
            }

            UpdateItems(dataGrid.ItemContainerGenerator, dataGrid.Items.Count, show);
        }

        private static void ItemContainerGenerator_ItemsChanged(object sender, ItemsChangedEventArgs e)
        {
            if (!(sender is ItemContainerGenerator generator)) return;
            UpdateItems(generator, e.ItemCount);
        }

        private static void UpdateItems(ItemContainerGenerator generator, int itemsCount, bool show = true)
        {
            if (show)
            {
                for (var i = 0; i < itemsCount; i++)
                {
                    var row = (DataGridRow)generator.ContainerFromIndex(i);
                    if (row != null)
                    {
                        row.Header = (i + 1).ToString();
                    }
                }
            }
            else
            {
                for (var i = 0; i < itemsCount; i++)
                {
                    var row = (DataGridRow)generator.ContainerFromIndex(i);
                    if (row != null)
                    {
                        row.Header = null;
                    }
                }
            }
        }

        private static void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e) => e.Row.Header = (e.Row.GetIndex() + 1).ToString();
    }
}
